﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Database;



namespace ATM
{
    public partial class manage : Form
    {

        private ConnectionDB cnn;
        FactoryDatabase factory = new CreateDB(TypeDB.MSSQL);
        public manage()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            groupNaptien.BringToFront();
            groupNaptien.Show();
            txtmatknap.Focus();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void manage_Load(object sender, EventArgs e)
        {
            cnn = factory.Create("A6E0J1PC7PNXIKK", "ATM");
            groupNaptien.Hide();
            string ten = "";
            DataTable a = new DataTable("admin");
            a = cnn.Query("select * from admin where acc = {0}", frmMain.user);
            DataRow b = a.Rows[0];
            ten = b["hoten"].ToString();
            labelhello.Text = "Xin chào "+ten;
            labelmatk.Text = taomatk();
            groupCaptk.BringToFront();
        }
        public string taomatk()
        {
            DataTable a = new DataTable("users");
            a = cnn.Query("select top 1 * from users ORDER BY matk DESC");
            DataRow b = a.Rows[0];
            string magoc = b["matk"].ToString();
            string mamoi = "";
            mamoi = (int.Parse(magoc) + 1).ToString();
            return mamoi;
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            groupCaptk.BringToFront();
            groupCaptk.Show();                       
            labelmatk.Text = taomatk();
            txtmapin.Focus();
        }

        private void btn_ok_new_Click(object sender, EventArgs e)
        {
            addnewuser();
        }

      
        private void addnewuser()
        {
            int checkbox, affectrow1,affectrow2;
            if (checkanb.Checked == true)
            {
                checkbox = 1;
            }
            else checkbox = 0;
            string a = txtns.Text;
            string[] b = a.Split('/');
            a = b[1] + "/" + b[0] + "/" + b[2];            
            affectrow1 = cnn.Execute("insert into users(matk,hoten,mapin,ns,diachi,sdt,cmt,enb) values ({0},{1},{2},{3},{4},{5},{6},{7})", labelmatk.Text,  txthoten.Text, txtmapin.Text, a,txtdc.Text, txtsdt.Text, txtcmt.Text, checkbox);
            affectrow2 = cnn.Execute("insert into usmoney(matk,mney) values ({0},0)",labelmatk.Text);
          
            if (affectrow1 > 0 && affectrow2 > 0)
            {
                MessageBox.Show("Đã thêm thành công");
            }
            else MessageBox.Show("Có lỗi xảy ra, vui lòng thử lại");


        }

        private void btn_clear_new_Click(object sender, EventArgs e)
        {
           
            txtmapin.Text = "";
            txthoten.Text = "";
            txtcmt.Text = "";
            txtdc.Text = "";
            txtns.Text = "";
            txtsdt.Text = "";
            checkanb.Checked = true;
        }

        private void btn_ok_nap_Click(object sender, EventArgs e)
        {
            int affectrow;
            affectrow = cnn.Execute("update usmoney set mney = mney + {0} where matk = {1}",txtsotiennap.Text,txtmatknap.Text);
            if (affectrow > 0)
            {
                MessageBox.Show("Đã thêm thành công");
                labelmoney.Text =  udtmoney();
                
            }
            else MessageBox.Show("Có lỗi xảy ra, vui lòng thử lại");
        }


        public string udtmoney()
        {
            DataTable a = new DataTable("usmoney");
            string returnmoney="0";
            a = cnn.Query("select * from usmoney where matk = {0}", txtmatknap.Text);
            if (a != null)
            {
                if (a.Rows.Count != 0)
                {
                    DataRow b = a.Rows[0];
                    returnmoney = b["mney"].ToString();
                    
                }

            }
            else MessageBox.Show("Tài khoản không tồn tại!");


            return returnmoney;
        }

        private void txtmatknap_TextChanged(object sender, EventArgs e)
        {
            labelmoney.Text =  udtmoney();
        }

        private void btn_xoa_nap_Click(object sender, EventArgs e)
        {
            txtmatknap.Text = "";
            txtsotiennap.Text = "";
            labelmoney.Text = "";
        }

        private void btn_info_Click(object sender, EventArgs e)
        {

            
            DataTable a = new DataTable("users");
            DataTable c = new DataTable("users");
            if (txt_suamatk.Text != "")
            {
                a = cnn.Query("select matk, mapin, hoten, convert(varchar,ns,103) as ns, cmt, diachi,  sdt, enb  from users where matk = {0}", txt_suamatk.Text);
                if (a != null)
                {
                    if (a.Rows.Count != 0)
                    {
                        DataRow b = a.Rows[0];
                        txt_suans.Text = b["ns"].ToString();
                        b = a.Rows[0];
                        txt_suahoten.Text = b["hoten"].ToString();
                        txt_suacmt.Text = b["cmt"].ToString();
                        txt_suadc.Text = b["diachi"].ToString();
                        txt_suasdt.Text = b["sdt"].ToString();
                        bool enable;
                        if (b["enb"].ToString() == "True")
                        {
                            enable = true;
                        }
                        else enable = false;
                        check_suaenb.Checked = enable;

                        txt_suadc.Enabled = true;
                        txt_suacmt.Enabled = true;
                        txt_suans.Enabled = true;
                        txt_suahoten.Enabled = true;
                        txt_suamapin.Enabled = true;
                        txt_suasdt.Enabled = true;
                        check_suaenb.Enabled = true;
                        txt_suamatk.Focus();

                    }
                    else
                    {
                        MessageBox.Show("Tài khoản không tồn tại!");
                        //txt_suamatk.Enabled = true;
                        txt_suamatk.Focus();
                        txt_suamatk.SelectAll();
                    } 
                }
                else
                {
                    //txt_suamatk.Enabled = true;
                    txt_suamatk.SelectAll();
                    MessageBox.Show("Tài khoản không tồn tại !");
                } 

            }
           
        }

        private void btn_suatk_Click(object sender, EventArgs e)
        {
            this.sua_clear();
            groupsuatk.BringToFront();
            groupsuatk.Show();
          
        }
        public void sua_clear()
        {
            txt_suamatk.Text = "";
            txt_suamapin.Text = "";            
            txt_suahoten.Text = "";
            txt_suans.Text = "";           
            txt_suacmt.Text = "";           
            txt_suadc.Text = "";            
            txt_suasdt.Text = "";
            txt_suamatk.Focus();
            txt_suamatk.Enabled = true;
            txt_suadc.Enabled = false;
            txt_suacmt.Enabled = false;
            txt_suans.Enabled = false;
            txt_suahoten.Enabled = false;
            txt_suamapin.Enabled = false;
            txt_suasdt.Enabled = false;
            check_suaenb.Enabled = false;
            check_suaenb.Checked = true;
    
        }
        private void button5_Click(object sender, EventArgs e)
        {
            this.sua_clear();
        }

        private void btn_delacc_Click(object sender, EventArgs e)
        {
            DialogResult a = MessageBox.Show("Bạn chắc chắn muốn xóa tài khoản này?", "Xóa tài khoản?", MessageBoxButtons.YesNo);
            if (a == DialogResult.Yes)
            {
                int affectrow1, affectrow2;
                affectrow1 = cnn.Execute("delete from usmoney where matk = {0}", txt_suamatk.Text);
                affectrow2 = cnn.Execute("delete from users where matk = {0}", txt_suamatk.Text);
                if (affectrow1 > 0 && affectrow2 > 0)
                {
                    MessageBox.Show("Đã xóa thành công tài khoản " +txt_suamatk.Text +"!");
                    this.sua_clear();
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            bool loi = false;
            
            int affectrow;
            if (txt_suamapin.Text != "")
            {
                affectrow = cnn.Execute("update users set mapin = {0} where matk = {1}",txt_suamapin.Text, txt_suamatk.Text);
                if (affectrow != 1)
                {
                    loi = true;
                    return;
                }
            }
            if (txt_suahoten.Text != "")
            {
                affectrow = cnn.Execute("update users set hoten = {0} where matk = {1}",txt_suahoten.Text, txt_suamatk.Text);
                if (affectrow != 1)
                {
                    loi = true;
                    return;
                }
            }
            if (txt_suans.Text != "")
            {
                string a = txt_suans.Text.ToString();
                string[] b = a.Split('/');
                a = b[1] + "/" + b[0] + "/" + b[2];               
                affectrow = cnn.Execute("update users set ns = {0} where matk = {1}", a, txt_suamatk.Text);
                if (affectrow != 1)
                {
                    loi = true;
                    return;
                }
            }
            if (txt_suacmt.Text != "")
            {
                affectrow = cnn.Execute("update users set cmt = {0} where matk = {1}", txt_suacmt.Text, txt_suamatk.Text);
                if (affectrow != 1)
                {
                    loi = true;
                    return;
                }
            }
            if (txt_suadc.Text != "")
            {
                affectrow = cnn.Execute("update users set diachi = {0} where matk = {1}", txt_suadc.Text, txt_suamatk.Text);
                if (affectrow != 1)
                {
                    loi = true;
                    return;
                }
            }
            if (txt_suasdt.Text != "")
            {
                affectrow = cnn.Execute("update users set sdt = {0} where matk = {1}", txt_suasdt.Text, txt_suamatk.Text);
                if (affectrow != 1)
                {
                    loi = true;
                    return;
                }
            }
            if (check_suaenb.Checked == true)
            {
                affectrow = cnn.Execute("update users set enb = 1 where matk = {0}", txt_suamatk.Text);
                if (affectrow != 1)
                {
                    loi = true;
                    return;
                }
            }
            else
            {
                affectrow = cnn.Execute("update users set enb = 0 where matk = {0}", txt_suamatk.Text);
                if (affectrow != 1)
                {
                    loi = true;
                    return;
                }
            }
            if (loi == true)
            {
                MessageBox.Show("Có lỗi xảy ra vui lòng thử lại!");
            }
            else
            {
                txt_suamapin.Text = "";
                MessageBox.Show("Lưu thành công");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            grouptimtk.BringToFront();
            grouptimtk.Show();            
            txt_inputtk.Focus();
            lsbox_dktk.SelectedItem = lsbox_dktk.Items[0];
        }

        private void btn_timkiem_Click(object sender, EventArgs e)
        {


            if (lsbox_dktk.SelectedItem != null)
            {
                if (txt_inputtk.Text != "")
                {
                    string texttk = txt_inputtk.Text;
                    string paramsearch = lsbox_dktk.SelectedItem.ToString();
                    string columnsch = "matk";
                    switch (paramsearch)
                    {
                        case "Mã tài khoản": columnsch = "matk";
                            break;
                        case "Họ tên": columnsch = "hoten";
                            break;
                        case "Địa chỉ": columnsch = "diachi";
                            break;
                        case "Số điện thoại": columnsch = "sdt";
                            break;
                        case "Số cmt": columnsch = "cmt";
                            break;
                         case "Ngày sinh": columnsch = "ns";
                            break;

                         default: columnsch = "matk";
                            break;
                    }
                    DataTable a = new DataTable("users");
                    a = cnn.Query("select a.matk as 'Mã tài khoản', a.hoten as 'Họ tên', a.diachi as 'Địa chỉ', a.sdt as 'Số điện thoại', a.cmt as 'Số cmt', convert(varchar,ns,103) as 'Ngày sinh', b.mney as 'Số dư tài khoản'from users as a join usmoney as b on a.matk = b.matk where a." + columnsch + " like {0}", "%" + texttk + "%");
                    if (a != null)
                    {
                        if (a.Rows.Count != 0)
                        {
                            dataGridtk.DataSource = a;
                            dataGridtk.Columns[0].Width = 110;
                            dataGridtk.Columns[1].Width = 150;
                            dataGridtk.Columns[2].Width = 180;
                            dataGridtk.Columns[3].Width = 100;
                            dataGridtk.Columns[4].Width = 80;
                            dataGridtk.Columns[5].Width = 80;
                            dataGridtk.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                        }
                        else
                        {
                            MessageBox.Show("Tài khoản không tồn tại!");                           
                            txt_inputtk.Focus();
                            txt_inputtk.SelectAll();
                        }

                    }
                    else
                    {
                        MessageBox.Show("Tài khoản không tồn tại!");
                        txt_inputtk.Focus();
                        txt_inputtk.SelectAll();
                    }
                }
                else
                {
                    MessageBox.Show("Bạn chưa nhập giá trị tìm kiếm");
                    txt_inputtk.Focus();
                } 

                

            }
            else MessageBox.Show("Không có điều kiện lọc nào được chọn");
           
        }

        private void btn_lk_suatk_Click(object sender, EventArgs e)
        {

            if (dataGridtk.SelectedRows.Count >0)
            {
                string matkselect = dataGridtk.SelectedRows[0].Cells[0].Value.ToString();
                btn_suatk.PerformClick();
                txt_suamatk.Text = matkselect;
                btn_info.PerformClick();
            }
        }

        private void txt_inputtk_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_timkiem.PerformClick();
            }
        }

     
       
     
      
    }
}
